package server

import (
	"errors"
	"gorm.io/gorm"
	"onerk4/zk2/common/dao/mysql"
	"onerk4/zk2/common/model"
	"onerk4/zk2/common/pkg"
	"onerk4/zk2/common/proto/user"
	"time"
)

func Login(req *user.LoginRequest) (*user.LoginResponse, error) {
	queryUser, err := mysql.QueryUser(req.Mobile)
	if err != nil {
		return nil, err
	}
	if queryUser.Password != req.Password {
		return nil, errors.New("password error")
	}
	claims := pkg.CustomClaims{
		ID: queryUser.ID,
	}
	token, err := pkg.NewJWT("2209a").CreateToken(claims)
	if err != nil {
		return nil, err
	}
	return &user.LoginResponse{
		Success: token,
	}, nil
}

func CourseList(req *user.CourseListRequest) (*user.CourseListResponse, error) {
	list, err := mysql.QueryCourseList()
	if err != nil {
		return nil, err
	}

	var result []*user.CourseItem

	for _, course := range list {
		result = append(result, &user.CourseItem{
			CourseId:   int64(course.ID),
			CourseName: course.CourseName,
			Stadium:    course.Stadium,
			Site:       course.Site,
			Time:       course.Time,
			Status:     int64(course.Status),
		})
	}

	return &user.CourseListResponse{List: result}, nil
}

func CourseInfo(req *user.CourseInfoRequest) (*user.CourseInfoResponse, error) {
	info, err := mysql.QueryCourseInfo(req.CourseId)
	if err != nil {
		return nil, err
	}
	return &user.CourseInfoResponse{
		CourseId:   int64(info.ID),
		CourseName: info.CourseName,
		Stadium:    info.Stadium,
		Site:       info.Site,
		Time:       info.Time,
		Status:     int64(info.Status),
	}, nil
}

func ReservationCourse(req *user.ReservationCourseRequest) (*user.ReservationCourseResponse, error) {
	info, err := mysql.QueryCourseInfo(req.CourseId)
	if err != nil {
		return nil, err
	}
	c := model.CourseReservation{
		Model:     gorm.Model{},
		UserId:    int(req.UserId),
		CourseId:  int(req.CourseId),
		Course:    info.CourseName,
		Date:      req.Data,
		Time:      info.Time,
		Stadium:   info.Stadium,
		Site:      info.Site,
		PeopleNum: int(req.PeopleNum),
		Coach:     req.Coach,
		Memo:      req.Memo,
		Status:    1,
	}
	err = c.ReservationCourse()
	if err != nil {
		return nil, err
	}

	return &user.ReservationCourseResponse{Success: "预约成功"}, nil
}

func QueryReservationCourse(req *user.QueryReservationCourseRequest) (*user.QueryReservationCourseResponse, error) {
	now := time.Now().AddDate(0, -6, 0)

	course, err := mysql.QueryReservationCourse(req.UserId, req.Status, now)
	if err != nil {
		return nil, err
	}

	var list []*user.Item

	for _, reservation := range course {
		list = append(list, &user.Item{
			CourseName: reservation.Course,
			Stadium:    reservation.Stadium,
			Site:       reservation.Site,
			Date:       reservation.Date,
			Time:       reservation.Time,
			Status:     int64(reservation.Status),
			PeopleNum:  int64(reservation.PeopleNum),
			Coach:      reservation.Coach,
		})
	}
	return &user.QueryReservationCourseResponse{List: list}, nil
}

func QueryReservationCourseInfo(req *user.QueryReservationCourseRequestInfoRequest) (*user.QueryReservationCourseResponseInfoResponse, error) {
	info, err := mysql.QueryReservationCourseInfo(req.ReservationId, req.UserId)
	if err != nil {
		return nil, err
	}
	now := time.Now()
	if now.Before(info.CreatedAt) {
		return nil, err
	}

	return &user.QueryReservationCourseResponseInfoResponse{Success: "您的课程还有30分钟就要开始了"}, nil
}

func DelQueryReservation(req *user.DelQueryReservationRequest) (*user.DelQueryReservationResponse, error) {
	info, err := mysql.QueryReservationCourseInfo(req.ReservationId, req.UserId)
	if err != nil {
		return nil, err
	}

	now := time.Now()
	if now.After(info.CreatedAt) {
		return nil, errors.New("您的课程开始时间小于一小时，不能取消预约")
	}

	err = mysql.DelQueryReservation(req.ReservationId, req.UserId)
	if err != nil {
		return nil, err
	}
	return &user.DelQueryReservationResponse{Success: "取消成功"}, nil
}
